package com.tang.k.wechatmessage.application.NewsList.view;

import android.os.Bundle;

import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.tang.k.wechatmessage.application.NewsList.modle.INews;
import com.tang.k.wechatmessage.application.NewsList.presenter.ArticlePresenterImpl;
import com.tang.k.wechatmessage.application.NewsList.view.adapter.ArticleAdapter;
import com.tang.k.wechatmessage.application.NewsList.view.adapter.DividerItemDecoration;
import com.tang.k.wechatmessage.application.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 简单实现，并没有考虑很多；
 * 很多生命周期应该处理，很多特殊情况需要处理
 */
public class ArticleActivity extends AppCompatActivity implements IArtivityView {
    @InjectView(R.id.acMain_recycler)
    RecyclerView acMainRecycler;
    @InjectView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private ArticleAdapter adapter;
    private List<INews> list = new ArrayList<>();
    ArticlePresenterImpl presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article);
        ButterKnife.inject(this);

        acMainRecycler.setLayoutManager(new LinearLayoutManager(this));
        acMainRecycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration
                .VERTICAL_LIST));
        adapter = new ArticleAdapter(this, list);
        acMainRecycler.setAdapter(adapter);

        //注入Presenter
        presenter = new ArticlePresenterImpl(this);

        //swipeRefreshLayout的刷新事件
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.refresh();
            }
        });

        //监听RecyclerView的Item点击事件
        adapter.setItemClickListener(new ArticleAdapter.ItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                String url = list.get(position).getUrl();
                presenter.openDeatailWebView(url);
            }
        });
    }

    /**
     * 当Activity可交互时才请求数据
     */
    @Override
    protected void onResume() {
        super.onResume();
        presenter.onResume();
    }

    /**
     * 接收从P传来的数据，更新和Adapter关联的List数据
     * 这个位置写的好丑陋
     */
    @Override
    public void onReceivedData(List<INews> data) {
        if (data == null) return;
        list.clear();
        for (INews news : data) {
            list.add(news);
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 接收P发来的【刷新失败】消息
     */
    @Override
    public void onRefreshFailure() {
        Snackbar.make(swipeRefreshLayout, "刷新失败", Snackbar.LENGTH_SHORT).show();
    }

    /**
     * 根据P传来的isRefreshing参数，设置swipeRefreshLayout
     */
    @Override
    public void onRefreshStateChanged(boolean isRefreshing) {
        swipeRefreshLayout.setRefreshing(isRefreshing);
    }

    /**
     * 将swipeRefreshLayout自身【是否正在刷新】传递给P
     */
    @Override
    public boolean getSwipeState() {
        return swipeRefreshLayout.isRefreshing();
    }

    /**
     * 将V正在执行OnDestroy的消息传递给P，让P释放资源，防止内存泄漏
     */
    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }

    /**
     * 将监听到的Back按键的触发事件传递给P
     */
    @Override
    public void onBackPressed() {
        presenter.handleBackPressed();
    }


}
